import Vue from 'vue'
import store from '../store/index'

export default {
  install(Vue) {
    Vue.prototype.webSocket = (url, flag, teamUrl, personUrl) => {
      let webSocket = new SockJS(url);
      let stompClient = Stomp.over(webSocket);
      stompClient.connect(
        {},
        function connectCallback(frame) {
          if (frame.command === "CONNECTED") {
            stompClient.subscribe(flag ? teamUrl : personUrl, (message) => {
              if (message.body) {
                store.commit('getWebSocketMsg', message.body)
              } else {
                console.log("got empty message");
              }
            });
          }
        }
        ,
        function errorCallBack() {
          // 连接失败时（服务器响应 ERROR 帧）的回调方法
          console.log("连接失败");
        }
      )
      ;
    }
  }
}
